package com.zh.media.controller;

import com.zh.common.annotation.NeedAuth;
import com.zh.common.annotation.NeedPermission;
import com.zh.common.annotation.NeedRoles;
import com.zh.common.annotation.Operator;
import com.zh.common.constant.AuthConstant;
import com.zh.media.domain.dto.AuditVideoDto;
import com.zh.media.domain.dto.PageVideoDto;
import com.zh.media.domain.dto.UpdateVideoDto;
import com.zh.media.domain.dto.UpdateWorkInfoDto;
import com.zh.web.base.PageApiResult;
import com.zh.web.domain.media.vo.VideoVo;
import com.zh.web.domain.media.entity.Video;
import com.zh.media.service.VideoService;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "视频管理接口")
@Slf4j
@RequestMapping("/video")
@RequiredArgsConstructor
@RestController
@Validated
@Operator
public class VideoController extends BaseController {

    private final VideoService videoService;

    @GetMapping
    @ApiOperation("id查视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    public ApiResult<VideoVo> getVideo(@RequestParam("id") Long id, HttpServletRequest request) {
        return buildSuccessPackage(videoService.getDetail(id, request));
    }

    @PostMapping("/page")
    @ApiOperation("分页查视频 标题 模糊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.ADMIN})}, permissions = {@NeedPermission(value = {AuthConstant.SELECT})})

    public ApiResult<PageApiResult<VideoVo>> pageVideo(@RequestBody PageVideoDto pageVideoDto) {
        return buildSuccessPackage(videoService.pageVideo(pageVideoDto));
    }

    @PostMapping("/list")
    @ApiOperation(value = "ids查询", hidden = true)
    public ApiResult<List<VideoVo>> listArticle(@RequestBody List<Long> ids) {
        return buildSuccessPackage(videoService.listVideoVoByIds(ids));
    }

    @GetMapping("/list/after")
    @ApiOperation(value = "查询时间之后的视频")
    public ApiResult<List<VideoVo>> listVideoAfterTime(LocalDateTime time) {
        return buildSuccessPackage(videoService.listVideoAfterTime(time));
    }

    @PostMapping("/upload")
    @ApiOperation("用户上传视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.USER})}, permissions = {@NeedPermission(value = {AuthConstant.INSERT})})
    public ApiResult<String> addVideo(
            @RequestParam("userId") Long userId,
            @RequestParam("title") String title,
            @RequestParam(value = "summary", required = false) String summary,
            @RequestParam("contentType") String contentType,
            @RequestParam("lawType") String lawType,
            @RequestParam("sourceType") Integer sourceType,
            @RequestParam(value = "source", required = false) String source,
            @RequestPart("file") MultipartFile file
    ) {
        Video video = new Video();
        video.setTitle(title);
        video.setSummary(summary);
        video.setContentType(contentType);
        video.setLawType(lawType);
        video.setSource(source);
        video.setSourceType(sourceType);
        video.setCreateOperator(userId);
        video.setUpdateOperator(userId);
        return buildSuccessPackage(videoService.uploadVideo(video, file));
    }

    @DeleteMapping
    @ApiOperation(value = "删除视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.USER})}, permissions = {@NeedPermission(value = {AuthConstant.DELETE})})
    public ApiResult<String> deleteVideo(@RequestParam("id") Long id) {
        return buildMsgPackage(videoService.deleteVideoById(id));
    }

    @PutMapping
    @ApiOperation(value = "修改视频信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.USER})}, permissions = {@NeedPermission(value = {AuthConstant.UPDATE})})
    public ApiResult<String> updateVideo(@RequestBody @Valid UpdateVideoDto updateVideoDto) {
        updateVideoDto.setUpdateOperator(operator);
        return buildMsgPackage(videoService.updateVideo(updateVideoDto));
    }

    @PutMapping("/status")
    @ApiOperation(value = "修改视频审核信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", dataType = "string", paramType = "header")
    })
    @NeedAuth(roles = {@NeedRoles(value = {AuthConstant.ADMIN})}, permissions = {@NeedPermission(value = {AuthConstant.UPDATE})})
    public ApiResult<String> statusAudit(@RequestBody AuditVideoDto auditVideoDto) {
        auditVideoDto.setUpdateOperator(operator);
        return buildMsgPackage(videoService.statusVideo(auditVideoDto));
    }

    //todo 视频的数据
     @PostMapping("/praise")
    @ApiOperation(value = "点赞/取消点赞视频",notes = "只传入praise")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.USER)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> praiseArticle(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto) {
        return buildMsgPackage(videoService.praiseVideo(updateWorkInfoDto));
    }

    @PostMapping("/collect")
    @ApiOperation(value = "收藏/取消收藏视频",notes = "只传入collect")
    @NeedAuth(roles = {@NeedRoles(value = AuthConstant.USER)})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "Authorization", value = "Bearer token", required = true, dataType = "string", paramType = "header")
    )
    public ApiResult<String> collectVideo(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto) {
        return buildMsgPackage(videoService.collectVideo(updateWorkInfoDto));
    }
    @PostMapping("/view")
    @ApiOperation(value = "更新浏览量,不传默认为1",notes = "只传入view")
    public ApiResult<Integer> updateView(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto){
        return buildSuccessPackage(videoService.updateView(updateWorkInfoDto));
    }
    @PostMapping("/share")
    @ApiOperation(value = "更新分享量,不传默认为1",notes = "只传入share")
    public ApiResult<Integer> updateShare(@Valid @RequestBody UpdateWorkInfoDto updateWorkInfoDto){
        return buildSuccessPackage(videoService.updateShare(updateWorkInfoDto));
    }
}
